//
// Created by YE on 2024/1/3.
//

#ifndef YCH_INTERPRETER_LEXER_H
#define YCH_INTERPRETER_LEXER_H

#include <variant>
#include "../common/ych.h"
#include "reader.h"
typedef YCH_STATE Lexer_State;
#define LEXER_OK 1
#define LEXER_ERROR 0
#define FIRST_REVERSED 257
enum TK_ {
    TK_TRUE = FIRST_REVERSED,
    TK_FALSE,
    TK_ELSE,
    TK_IF,
    TK_WHILE,
    TK_FUNCTION,
    TK_RETURN,
    TK_INT,
    TK_FLOAT,
    TK_MAT,
    TK_STRING,
    TK_BOOL,
    TK_DEL,
    TK_BREAK,
    /* other token */
    TK_NAME,
    TK_INT_V,
    TK_FLOAT_V,
    TK_STRING_V,
    TK_NOTEQUAL,
    TK_EQUAL,
    TK_GREATEREQUAL,
    TK_LESSEQUAL,
    TK_INNER,
    TK_POW,
    TK_EOF,
    TK_ILLEGAL,
    LAST_TK_,
};
typedef unsigned int State;

typedef YCH_INT Int;
typedef YCH_FLOAT Float;
typedef YCH_STRING String;

typedef union Num_Info {
    Int i;
    Float f;
} Num_Info;

enum CType {
    letter, digit0, digit_, single, dot, eq, ast, ex, qm, blank, enter, greater, less, eof, illegal
};

static TK_ get_Token_Type(String &s);

static CType get_CType(char ch);

class Token {
public:
    Token();

    ~Token();

    unsigned int type;// token enum value
    Num_Info n;
    std::string s;
};


static State change(State &state, CType cType);

class Lexer {
public:
    Lexer(Reader *reader);

    ~Lexer();

    Lexer_State get_next_Token(Token *token);

    String error_string(size_t start_pos, State error_type) const;

    Reader *reader;
private:
    void skip_blanks() const;

};

#endif //YCH_INTERPRETER_LEXER_H

